﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WMS_Model;

namespace WMS_Business
{
    public class RequestManage
    {
        /// <summary>
        /// 根据工位名称查询工位编号接口
        /// </summary>
        private string StationNum 
        {
            get 
            {
                return "http://localhost:61542/api/StationGet";
            }
        }
        /// <summary>
        /// 工位名称查询url
        /// </summary>
        private string StationqueryURL
        {
            get
            {
                return "http://localhost:61542/api/StationGetAllName";
            }
        }
        /// <summary>
        /// 物料名称查询url
        /// </summary>
        private string MaterialqueryURL
        {
            get
            {
                return "http://localhost:61542/api/MaterialGetAllName";
            }
        }
        /// <summary>
        /// 查询任务
        /// </summary>
        private string Taskquery
        {
            get
            {
                return "http://localhost:61542/api/TaskGet";
            }
        }
        /// <summary>
        /// 呼料放料任务
        /// </summary>
        private string MateriaCall
        {
            get
            {
                return "http://localhost:61542/api/TaskAdd";
            }
        }
        
        HttpWeb http;
        public RequestManage()
        {
            http = new HttpWeb();
        }
        /// <summary>
        /// 绑定工位下拉列表框
        /// </summary>
        public List<string> Stationlist()
        {
            string name = http.HttpPost(StationqueryURL, "");
            List<string> StationName = JsonConvert.DeserializeObject<List<string>>(name);
            return StationName;
        }
        /// <summary>
        /// 绑定物料下拉列表框
        /// </summary>
        /// <returns></returns>
        public List<string> Materiallist()
        {
            string name = http.HttpPost(MaterialqueryURL, "");
            List<string> MaterialName = JsonConvert.DeserializeObject<List<string>>(name);
            return MaterialName;
        }
        /// <summary>
        /// 根据条件查询任务
        /// </summary>
        /// <param name="name">工位名称</param>
        /// <param name="type">工位类型</param>
        /// <returns></returns>
        public List<TasksTable> Tasks(string name,string type)
        {
            TaskURL task = new TaskURL
            {
                StationName = name,
                TaskType=type
            };
            string json = JsonConvert.SerializeObject(task);
            string tables = http.HttpPost(Taskquery, json);
            return JsonConvert.DeserializeObject<List<TasksTable>>(tables);
        }
        /// <summary>
        /// 呼料放料方法
        /// </summary>
        /// <param name="Rise">起点</param>
        /// <param name="End">终点</param>
        /// <param name="name">物料名称</param>
        /// <param name="type">任务类型</param>
        /// <param name="num">数量</param>
        /// <returns></returns>
        public string TasksAdd(string Rise, string End, string name,string type, int num)
        {

           string stationRise= http.HttpPost(StationNum, JsonConvert.SerializeObject(Rise));
           string stationEnd = http.HttpPost(StationNum, JsonConvert.SerializeObject(End));
            TasksTable tasks = new TasksTable
            {
                TaskNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
                TaskType = type,
                TaskStatus = "未执行",
                MaterialName = name,
                MaterialNumber = num,
                RiseSpot = int.Parse(stationRise),
                EndSpot = int.Parse(stationEnd),
                StartTime = DateTime.Now,
                EndTime=DateTime.Now
            };

            return http.HttpPost(MateriaCall,JsonConvert.SerializeObject(tasks));
        }
    }
}
